package com.zhang.exception;

import com.zhang.responce.Result;
import com.zhang.responce.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;

/**
 * @program: xinguan-parent
 * @description: 全局异常处理
 * @author: Mr.Zhang
 * @create: 2022-03-02 14:42
 **/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler{
    /**
     * 功能描述: <br>
     * 〈算数异常处理〉
     * @Param: [e]
     * @Return: com.zhang.responce.Result
     * @Author: Mr.zhang
     * @Date: 2022/3/2 15:13
     */
    @ExceptionHandler(ArithmeticException.class)
    public Result arithmeticException(ArithmeticException e){
        log.info(ResultCode.ARITHMETIC_EXCEPTION.getMessage()+e.getMessage());
        e.printStackTrace();
        return Result.error(ResultCode.ARITHMETIC_EXCEPTION);
    }
    /**
     * 功能描述: <br>
     * 〈空指针〉
     * @Param: [e]
     * @Return: com.zhang.responce.Result
     * @Author: Mr.zhang
     * @Date: 2022/3/2 15:15
     */
    @ExceptionHandler(NullPointerException.class)
    public Result nullPointerException(NullPointerException e){
        log.info(ResultCode.PARAM_IS_BLANK.getMessage()+e.getMessage());
        e.printStackTrace();
        return Result.error(ResultCode.PARAM_IS_BLANK);
    }

    @ExceptionHandler(RuntimeException.class)
    public Result runtimeException(RuntimeException e){
        log.info(ResultCode.PARAM_IS_BLANK.getMessage()+e.getMessage());
        e.printStackTrace();
        return Result.error(ResultCode.PARAM_IS_BLANK);
    }

    /**
     * 功能描述: <br>
     * 〈其他异常〉
     * @Param: [e]
     * @Return: com.zhang.responce.Result
     * @Author: Mr.zhang
     * @Date: 2022/3/2 15:17
     */
    @ExceptionHandler(Exception.class)
    public Result exception(Exception e){
        log.info(ResultCode.COMMON_FAIL.getMessage()+e.getMessage());
        e.printStackTrace();
        return Result.error(ResultCode.COMMON_FAIL);
    }

    /**
     * 功能描述: <br>
     * 〈业务类异常〉
     * @Param: [e]
     * @Return: com.zhang.responce.Result
     * @Author: Mr.zhang
     * @Date: 2022/3/2 15:21
     */
    @ExceptionHandler(BusinessException.class)
    public Result businessException(BusinessException e){
        log.info(e.getErrMsg()+e.getMessage());
        e.printStackTrace();
        return Result.error().code(e.getCode()).message(e.getErrMsg());
    }

    /**
     * 功能描述: <br>
     * 〈IO错误〉
     * @Param: [e]
     * @Return: com.zhang.responce.Result
     * @Author: Mr.zhang
     * @Date: 2022/3/8 16:04
     */
    @ExceptionHandler(IOException.class)
    public Result iOException(IOException e){
        return Result.error(ResultCode.IO_EXCEPTION);
    }

}
